Org provides
a special hook that can be used to narrow down the selection made
by these agenda views: todo, alltodo,
tags, tags-todo,
tags-tree. You may specify a function that is used
at each match to verify if the match should indeed be part of the
agenda view, and if not, how much should be skipped.
Let's say you want to produce a list of projects that contain a WAITING tag anywhere in the project tree. Let's further assume that you have marked all tree headings that define a project with the TODO keyword PROJECT. In this case you would run a TODO search for the keyword PROJECT, but skip the match unless there is a WAITING tag anywhere in the subtree belonging to the project line.
To achieve this, you must write a function that searches the
subtree for the tag. If the tag is found, the function must
return nil to indicate that this match should not be
skipped. If there is no such tag, return the location of the end
of the subtree, to indicate that search should continue from
there.
(defun my-skip-unless-waiting ()
"Skip trees that are not waiting"
(let ((subtree-end (save-excursion (org-end-of-subtree t))))
(if (re-search-forward ":waiting:" subtree-end t)
nil ; tag found, do not skip
subtree-end))) ; tag not found, continue after end of subtree
Now you may use this function in an agenda custom command, for example like this:
(org-add-agenda-custom-command
'("b" todo "PROJECT"
((org-agenda-skip-function 'my-skip-unless-waiting)
(org-agenda-overriding-header "Projects waiting for something: "))))
Note
that this also binds org-agenda-overriding-header to
get a meaningful header in the agenda view.
A general
way to create custom searches is to base them on a search for
entries with a certain level limit. If you want to study all
entries with your custom search function, simply do a search for
‘LEVEL>0’1, and then use
org-agenda-skip-function to select the entries you
really want to have.
You may also put a Lisp form into
org-agenda-skip-function. In particular, you may use
the functions org-agenda-skip-entry-if and
org-agenda-skip-subtree-if in this form, for
example:
'(org-agenda-skip-entry-if 'scheduled)'(org-agenda-skip-entry-if 'notscheduled)'(org-agenda-skip-entry-if 'deadline)'(org-agenda-skip-entry-if 'scheduled
'deadline)'(org-agenda-skip-entry-if 'todo '("TODO"
"WAITING"))'(org-agenda-skip-entry-if 'todo 'done)'(org-agenda-skip-entry-if 'timestamp)'(org-agenda-skip-entry 'regexp "regular
expression")'(org-agenda-skip-entry 'notregexp "regular
expression")'(org-agenda-skip-subtree-if 'regexp "regular
expression")Therefore we could also have written the search for WAITING projects like this, even without defining a special function:
(org-add-agenda-custom-command
'("b" todo "PROJECT"
((org-agenda-skip-function '(org-agenda-skip-subtree-if
'regexp ":waiting:"))
(org-agenda-overriding-header "Projects waiting for something: "))))
[1] Note that, when using
org-odd-levels-only, a level number corresponds to
order in the hierarchy, not to the number of stars.